package com.xgc.book.bookserver.controller;


import com.xgc.book.bookserver.bean.UserinfoBean;
import com.xgc.book.bookserver.service.UserinfoService;
import com.xgc.book.bookserver.util.ResultJson;
import com.xgc.book.bookserver.util.form.PasswordForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户登录表 前端控制器
 * </p>
 *
 * @author wrz
 * @since 2023-11-20
 */
@RestController
@RequestMapping("/userinfo")
public class UserinfoController extends BaseLoginController {
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("/page")
    public ResultJson list(int page, int limit, UserinfoBean userinfo) {
        return userinfoService.getPage(page, limit, userinfo);
    }

    @GetMapping("/info/{userId}")
    public ResultJson info(@PathVariable Integer userId) {
        return ResultJson.ok(userinfoService.getById(userId));
    }

    @GetMapping("/info")
    public ResultJson info() {
        return ResultJson.ok(userinfoService.getById(getLoginId()));
    }

    @PostMapping(value = {"/update", "/status"})
    public ResultJson update(@RequestBody UserinfoBean userinfo) {
        userinfoService.update(userinfo);
        return ResultJson.ok();
    }

    @PostMapping("/password")
    public ResultJson password(@RequestBody PasswordForm passwordForm) {
        passwordForm.setUserId(getLoginId());
        userinfoService.updatePassword(passwordForm);
        return ResultJson.ok();
    }

    @DeleteMapping("/delete/{userId}")
    public ResultJson delete(@PathVariable Integer userId) {
        if (userId == 1) {
            return ResultJson.error("管理员禁止删除！");
        }
        userinfoService.removeById(userId);
        return ResultJson.ok();
    }

    @GetMapping("/author/select")
    public ResultJson authorList() {
        return userinfoService.getAuthorList();
    }
}

